package nus.mtech.customer;

import java.util.HashMap;
import java.util.ArrayList;
import java.util.Map;

/**
 * @author Kyaw Phyu Aung, Gaurav Pahwa
 *
 */
public class ComplaintManager {

	private static ComplaintManager objComplaintMgr;
	private Map<String, ArrayList<Complaint>> complaintMap = new HashMap<String, ArrayList<Complaint>>();
	private static int complaintId = 1;

	public static ComplaintManager getComplaintManagerInstance() {
		if (null == objComplaintMgr) {
			objComplaintMgr = new ComplaintManager();
		}
		return objComplaintMgr;
	}

	private ComplaintManager() {
		super();
		objComplaintMgr = this;
	}

	public void addNewComplaint(String accountNo, Complaint complaint) {
		if (complaintMap.containsKey(accountNo)) {
			complaintMap.get(accountNo).add(complaint);
		} else {
			ArrayList<Complaint> aListComplaint = new ArrayList<Complaint>();
			aListComplaint.add(complaint);
			complaintMap.put(accountNo, aListComplaint);
		}
	}

	public ArrayList<Complaint> getComplaintByAccountNo(String accountNo) {
		if (complaintMap.containsKey(accountNo)) {
			return (ArrayList<Complaint>) complaintMap.get(accountNo);
		} else {
			return null;
		}
	}

	public Complaint getComplaintByComplaintID(String accountNo, int complaintID) {
		if (complaintMap.containsKey(accountNo)) {
			ArrayList<Complaint> alistComplaint = complaintMap.get(accountNo);
			for (Complaint c : alistComplaint) {
				if (c.getComplaintID() == complaintID) {
					return c;
				}
			}
		}
		return null;
	}

	public ArrayList<Complaint> getComplaintByNRIC(String custNRIC) {
		CustomerManager custMgr = CustomerManager.getCustomerManagerInstance();
		String accountNo = custMgr.getCustomerByNRIC(custNRIC).getAccountNo();

		if (!"".equals(accountNo)) {
			return this.getComplaintByAccountNo(accountNo);
		}
		return null;
	}

	public void removeComplaint(String accountNo, int complaintID) {
		Complaint c = this.getComplaintByComplaintID(accountNo, complaintID);
		if (c != null) {
			c.setStatus(false);
		}
	}

	public int updateComplaint(String accountNo, int complaintID,
			String compFinding, Boolean status) {
		int updtCount = 0;
		Complaint c = this.getComplaintByComplaintID(accountNo, complaintID);

		if (c != null) {
			c.setStatus(status);
			c.setComplaintFinding(compFinding);
			updtCount++;
		}
		return updtCount;
	}

	public int getComplaintId() {
		return complaintId++;
	}

	public void setComplaintId(int complaintId) {
		ComplaintManager.complaintId = complaintId;
	}

	public Map<String, ArrayList<Complaint>> getComplaintMap() {
		return complaintMap;
	}

	public void setComplaintMap(Map<String, ArrayList<Complaint>> complaintMap) {
		this.complaintMap = complaintMap;
	}

}
